﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NextCMS.Models;

public partial class WebAdmin_ManageContentPages : WebadminBasePage
{
    int Id = 0;
    bool IsDefaultSite = false;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(SiteId) == SiteSettingsHelper.GetSetting("DefaultSite", 0))
            IsDefaultSite = true;
        
        int.TryParse(Convert.ToString(Request.QueryString["Id"]), out Id);
        if (!IsPostBack)
        {
            string command = Convert.ToString(Request.QueryString["Type"]);
            ExecuteCommand(command);
            BindPages();
        }
    }
    private void ExecuteCommand(string command)
    {
        if (!String.IsNullOrEmpty(command) && command == "Delete")
        {
            PageModulesHelper pmc = new PageModulesHelper();
            IEnumerable<PageModuleModel> lstPageModel= pmc.GetByPageId(Id);
            foreach (PageModuleModel pmm in lstPageModel)
                pmc.Delete(pmm.Id);

            PagesHelper cc = new PagesHelper();
            cc.Delete(Id);
        }
        else if (!String.IsNullOrEmpty(command) && command == "HomePage")
        {
            SiteSettingsHelper.SetSetting("HomePage", Id.ToString(),SiteId);
        }
        
    }
    public string IsHomePage(string Id)
    {
        if (SiteSettingsHelper.GetSetting("HomePage",SiteId) == Id)
            return "../App_Themes/WebAdmin/buttons/radio.png";
        else
            return "../App_Themes/WebAdmin/buttons/radiobutton_no.png";
    }
    public string PageURL(bool IsLayoutMode, string fileName)
    {
        if (IsDefaultSite)
            if (IsLayoutMode)
                return "../Layout_" + fileName + "";
            else
                return "../" + fileName + "";
        else
            if (IsLayoutMode)
                return "../" + SiteName + "/Layout_" + fileName + "";
            else
                return "../" + SiteName + "/" + fileName + "";
    }
    private void BindPages()
    {
        using (PagesHelper pc = new PagesHelper())
        {
            List<PageModel> lstPages =pc.GetAllBySiteId(SiteId).ToList();
            List<PageModel> lstPageModel = pc.GetAllBySiteIdLanguageId(SiteId, LanguageId).ToList();

            lstPages = lstPages.Where(c => !lstPageModel.Select(s => s.Id).Contains(c.Id)).ToList();

            lstPageModel = lstPageModel.Concat(lstPages).ToList();
            foreach (PageModel pm in lstPageModel)
            {
                using (TemplatesHelper mpc = new TemplatesHelper())
                {
                    TemplateModel tm = mpc.GetById(pm.MasterPageId);
                    if (tm != null)
                        pm.MasterPageName = tm.Name;
                }
            }
            GVPages.DataSource = lstPageModel;
            GVPages.DataBind();
        }
    }
    
    
}